// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    pos_y: number = 0;
    speed: number = 0;
    state: number = 0;
    @property(cc.Integer)
    gravity: number = 0.5;
    @property(cc.Integer)
    initial_speed: number = 10
    @property(cc.Canvas)
    touch_canvas: cc.Canvas = null;

    touch(event:cc.Event.EventTouch){
        this.state =(this.state + 1)%2;
    }

    onLoad () {
        this.pos_y = this.node.y;
        this.speed = this.initial_speed;
        this.touch_canvas.node.on(cc.Node.EventType.TOUCH_END,this.touch,this)
    }

    start () {

    }

    update (dt:number) {
        if( this.state== 0)
        this.node.y += this.speed;
        this.speed -= this.gravity;
        if(this.node.position.y <= this.pos_y){
            this.node.y = this.pos_y;
            this.speed = this.initial_speed;
        }
        
    }
}
